package com.lalifa.googledate.ui.activity.type

import androidx.recyclerview.widget.ItemTouchHelper
import com.drake.brv.BindingAdapter
import com.drake.brv.listener.DefaultItemTouchCallback
import com.lalifa.base.BaseTitleActivity
import com.lalifa.extension.getIntentSerializable
import com.lalifa.extension.hideKeyboard
import com.lalifa.extension.onClick
import com.lalifa.googledate.R
import com.lalifa.googledate.api.CalendarBean
import com.lalifa.googledate.api.CalendarUp
import com.lalifa.googledate.databinding.ActivityTypeManageBinding
import com.lalifa.googledate.ui.adapter.calendarInfoAdapter
import com.lalifa.googledate.ui.dialog.showAddTypeDialog
import com.lalifa.googledate.util.CalendarUtils
import com.lalifa.utils.LiveDataBus

class TypeManage : BaseTitleActivity<ActivityTypeManageBinding>() {
    override fun getViewBinding() = ActivityTypeManageBinding.inflate(layoutInflater)
    override fun title() = getString(R.string.type_management)
    override fun initView() {
        val list = getIntentSerializable<CalendarBean>("list") as List<CalendarBean>
        val adapter = binding.list.calendarInfoAdapter().apply {
            itemTouchHelper = ItemTouchHelper(object : DefaultItemTouchCallback() {
                override fun onDrag(
                    source: BindingAdapter.BindingViewHolder,
                    target: BindingAdapter.BindingViewHolder
                ) {
                    super.onDrag(source, target)
                    CalendarUtils.CALENDARS_LIST = models as ArrayList<CalendarBean>
                }
            })
            R.id.changeCalendar.onClick {
                val bean = getModel<CalendarBean>()
                showAddTypeDialog(bean.id.toString(), bean.name, bean.icon, bean.color)
            }
        }
        adapter.models = list
        LiveDataBus.get().with("calenderList", CalendarUp::class.java)
            .observe(this) { event ->
                hideKeyboard(binding.addType)
                CalendarUtils.upCalendarList(this)
                adapter.models = CalendarUtils.CALENDARS_LIST
            }
    }

    override fun onClick() {
        super.onClick()
        binding.apply {
            addType.onClick { showAddTypeDialog() }
        }
    }
}